﻿#region
/**
 * $Date: 0001/1/1
*/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using BuXiaChu.OrderDishes.DAL.DataAccess;
using BuXiaChu.OrderDishes.Model;
#endregion

namespace BuXiaChu.OrderDishes.DAL
{
    public class DishesDAL
    {
        #region private const
        private const string PARAM_CODE_ID = "@CodeId";
        private const string PARAM_RESTAURANT_ID = "@RestaurantId";
        private const string PARAM_CAT_I = "@CatID";
        private const string PARAM_NAME = "@Name";
        private const string PARAM_UNIT = "@Unit";
        private const string PARAM_PRICE = "@Price";
        private const string PARAM_DISCOUNT = "@Discount";
        private const string PARAM_INVOICE_DISCOUNT = "@InvoiceDiscount";
        private const string PARAM_DESCRIPTION = "@Description";
        private const string PARAM_IMAGE_NAME = "@ImageName";
        private const string PARAM_IMAGE = "@Image";
        private const string PARAM_STATUS = "@Status";
        private const string PARAM_INTERNAL_REMARKS = "@InternalRemarks";
        private const string PARAM_BOX = "@Box";
        private const string PARAM_BOX_UNIT_PRICE = "@BoxUnitPrice";
        private const string PARAM_CREATE_DATE = "@CreateDate";
        private const string PARAM_UPDATE_DATE = "@UpdateDate";

        private static readonly string ConnectionString = SQLHelper.CONNECTION_STRING;
        #endregion

        private static void FillModelFrom(SqlDataReader reader, DishesModel obj)
        {
            #region
            if (reader != null && !reader.IsClosed)
            {
                obj.CodeId = reader.IsDBNull(reader.GetOrdinal("CodeId")) ? 0 : reader.GetDecimal(reader.GetOrdinal("CodeId"));
                obj.RestaurantId = reader.IsDBNull(reader.GetOrdinal("RestaurantId")) ? 0 : reader.GetInt32(reader.GetOrdinal("RestaurantId"));
                obj.CatID = reader.IsDBNull(reader.GetOrdinal("CatID")) ? 0 : reader.GetInt32(reader.GetOrdinal("CatID"));
                obj.Name = reader.IsDBNull(reader.GetOrdinal("Name")) ? String.Empty : reader.GetString(reader.GetOrdinal("Name"));
                obj.Unit = reader.IsDBNull(reader.GetOrdinal("Unit")) ? 0 : reader.GetInt32(reader.GetOrdinal("Unit"));
                obj.Price = reader.IsDBNull(reader.GetOrdinal("Price")) ? 0 : reader.GetDecimal(reader.GetOrdinal("Price"));
                obj.Discount = reader.IsDBNull(reader.GetOrdinal("Discount")) ? 0 : reader.GetInt32(reader.GetOrdinal("Discount"));
                obj.InvoiceDiscount = reader.IsDBNull(reader.GetOrdinal("InvoiceDiscount")) ? 0 : reader.GetInt32(reader.GetOrdinal("InvoiceDiscount"));
                obj.Description = reader.IsDBNull(reader.GetOrdinal("Description")) ? String.Empty : reader.GetString(reader.GetOrdinal("Description"));
                obj.ImageName = reader.IsDBNull(reader.GetOrdinal("ImageName")) ? String.Empty : reader.GetString(reader.GetOrdinal("ImageName"));
                obj.Image = reader.IsDBNull(reader.GetOrdinal("Image")) ? new byte[0] { } : reader.GetValue(reader.GetOrdinal("Image")) as byte[];
                obj.Status = reader.IsDBNull(reader.GetOrdinal("Status")) ? Convert.ToByte(0) : reader.GetByte(reader.GetOrdinal("Status"));
                obj.InternalRemarks = reader.IsDBNull(reader.GetOrdinal("InternalRemarks")) ? String.Empty : reader.GetString(reader.GetOrdinal("InternalRemarks"));
                obj.Box = reader.IsDBNull(reader.GetOrdinal("Box")) ? 0 : reader.GetInt32(reader.GetOrdinal("Box"));
                obj.BoxUnitPrice = reader.IsDBNull(reader.GetOrdinal("BoxUnitPrice")) ? 0 : reader.GetDecimal(reader.GetOrdinal("BoxUnitPrice"));
                obj.CreateDate = reader.IsDBNull(reader.GetOrdinal("CreateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("CreateDate"));
                obj.UpdateDate = reader.IsDBNull(reader.GetOrdinal("UpdateDate")) ? DateTime.MinValue : reader.GetDateTime(reader.GetOrdinal("UpdateDate"));
            }
            #endregion
        }

        private static SqlParameter[] GetDishesParams(DishesModel obj)
        {
            #region
            SqlParameter[] dbParams ={					
					 SQLHelper.MakeParam(PARAM_CODE_ID, SqlDbType.Decimal,0,obj.CodeId),
					 SQLHelper.MakeParam(PARAM_RESTAURANT_ID, SqlDbType.Int,0,obj.RestaurantId),
					 SQLHelper.MakeParam(PARAM_CAT_I, SqlDbType.Int,0,obj.CatID),
					 SQLHelper.MakeParam(PARAM_NAME, SqlDbType.NVarChar, 20,obj.Name),					
					 SQLHelper.MakeParam(PARAM_UNIT, SqlDbType.Int,0,obj.Unit),
					 SQLHelper.MakeParam(PARAM_PRICE, SqlDbType.Money,0,obj.Price),
					 SQLHelper.MakeParam(PARAM_DISCOUNT, SqlDbType.Int,0,obj.Discount),
					 SQLHelper.MakeParam(PARAM_INVOICE_DISCOUNT, SqlDbType.Int,0,obj.InvoiceDiscount),
					 SQLHelper.MakeParam(PARAM_DESCRIPTION, SqlDbType.NVarChar, 100,obj.Description),					
					 SQLHelper.MakeParam(PARAM_IMAGE_NAME, SqlDbType.NVarChar, 50,obj.ImageName),					
					 SQLHelper.MakeParam(PARAM_IMAGE, SqlDbType.Image,0,obj.Image),
					 SQLHelper.MakeParam(PARAM_STATUS, SqlDbType.TinyInt,0,obj.Status),
					 SQLHelper.MakeParam(PARAM_INTERNAL_REMARKS, SqlDbType.NVarChar, 100,obj.InternalRemarks),					
					 SQLHelper.MakeParam(PARAM_BOX, SqlDbType.Int,0,obj.Box),
					 SQLHelper.MakeParam(PARAM_BOX_UNIT_PRICE, SqlDbType.Money,0,obj.BoxUnitPrice)
			};

            return dbParams;
            #endregion
        }

        public static bool Save(DishesModel obj)
        {
            #region
            try
            {
                SqlParameter[] dbParams = GetDishesParams(obj);

                if (obj.CodeId == 0)
                {
                    obj.CodeId = Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Dishes_InsertUpdate", dbParams));
                    return obj.CodeId > 0 ? true : false;
                }
                else
                {
                    var affectNum = SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Dishes_InsertUpdate", dbParams);
                    return affectNum > 0 ? true : false;
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Dishes-->InsertOrUpdate-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }

            return false;
            #endregion
        }

        public static DishesModel GetDishesById(decimal codeId)
        {
            #region
            SqlDataReader reader = null;
            DishesModel obj = new DishesModel();

            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CODE_ID, SqlDbType.Int, 0, codeId)
                    };

                reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Dishes_GetDishesById", dbParams);

                if (reader.Read())
                {
                    FillModelFrom(reader, obj);
                }
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Dishes-->GetSingleObject-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            finally
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();
            }

            return obj;
            #endregion
        }

        public static bool Delete(decimal codeId)
        {
            #region
            try
            {
                SqlParameter[] dbParams = {
                        SQLHelper.MakeParam(PARAM_CODE_ID, SqlDbType.Int, 0, codeId)
                    };

                return SQLHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Dishes_Delete", dbParams) > 0 ? true : false;
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Dishes-->Delete-->" + e.Message);

                ExceptionMessageDAL.Record(ex);
            }
            return false;
            #endregion
        }

        public static List<DishesModel> Search(string sqlWhere, int pageIndex, int pageSize, out int pageCount)
        {
            #region
            List<DishesModel> result = new List<DishesModel>();
            pageCount = 0;
            try
            {
                if (string.IsNullOrEmpty(sqlWhere))
                {
                    sqlWhere = " 1=1 ";
                }

                SqlParameter[] dbParams =
                {
                    SQLHelper.MakeParam("@sqlWhere", SqlDbType.NVarChar,0,sqlWhere),
                    SQLHelper.MakeParam("@pageIndex",SqlDbType.Int,0,pageIndex),
                    SQLHelper.MakeParam("@pageSize",SqlDbType.Int,0,pageSize),
                    SQLHelper.MakeOutParam("@pageCount", SqlDbType.Int,0,0)                    
                };
                using (SqlDataReader reader = SQLHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Dishes_Search", dbParams))
                {
                    while (reader.Read())
                    {
                        DishesModel model = new DishesModel();
                        FillModelFrom(reader, model);
                        result.Add(model);
                    }
                }
                pageCount = int.Parse(dbParams[3].Value.ToString());
            }
            catch (Exception e)
            {
                Exception ex = new Exception("Dishes-->Search-->" + e.ToString());
                ExceptionMessageDAL.Record(ex);
            }
            return result;
            #endregion
        }
    }
}
